from pyparsing import null_debug_action


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or head.next==None:
            return head
        
        node = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return node

    def reverseList_2(self, head: ListNode) -> ListNode:
        if not head or head.next==None:
            return head
        
        pre = None
        cur = head
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre